package com.spsoft.vatoutinv.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.spsoft.core.annotation.AnonymousAccess;
import com.spsoft.core.annotation.Log;
import com.spsoft.core.controller.BaseController;
import com.spsoft.core.enums.LogActionType;
import com.spsoft.core.enums.LogMenuEnum;
import com.spsoft.core.model.Result;
import com.spsoft.core.model.bo.TokenUser;
import com.spsoft.core.model.leqidmz.param.BaseNsrRequestParams;
import com.spsoft.core.model.leqidmz.param.QueryCreditPeriodParams;
import com.spsoft.vatoutinv.constants.CommonConstant;
import com.spsoft.vatoutinv.model.dto.vmscreditsend.VmsCreditUpdateDto;
import com.spsoft.vatoutinv.model.vo.vmscreditlimit.VmsCreditlimitVO;
import com.spsoft.vatoutinv.service.vmscreditlimit.VmsCreditLimitService;
import com.spsoft.vatoutinv.service.vmsenterpriseinfo.VmsEnterpriseInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * 授信额度信息表 前端控制器
 * </p>
 *
 * @author wangy
 * @since 2023-11-24
 */
@Api(tags = {"授信额度信息"})
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/vmsCreditLimit")
public class VmsCreditLimitController extends BaseController {

    public static final String LOG_MSG = "(授信额度信息控制层)";

    private final VmsCreditLimitService creditLimitService;

    private final VmsEnterpriseInfoService vmsEnterpriseInfoService;

    @ApiOperation("授信额度-调用接口查询授信额度")
    @AnonymousAccess(value = "/addInfo", method = RequestMethod.POST, permissionCode = "VMS_CREDIT_LIMIT_ADD")
    @Log(value = "授信额度-调用接口查询授信额度", type = LogActionType.ADD, menuName = LogMenuEnum.CREDIT_QUOTA)
    public Result<Object> getCreditLimitByLq() {
        TokenUser tokenUser = getUser();
        BaseNsrRequestParams params = new BaseNsrRequestParams();
        params.setNsrsbh(vmsEnterpriseInfoService.getNsrsbh());
        params.setCreateUser(tokenUser.getUserCode());
        params.setDeptCode(tokenUser.getDeptCode());
        log.info("{}调用乐企接口查询授信额度，入参：{}", LOG_MSG, JSONObject.toJSONString(params));
        return creditLimitService.getCreditLimitByLq(params, tokenUser);
    }

    @ApiOperation("授信额度-本地存储授信额度查询")
    @AnonymousAccess(value = "/getInfo", method = RequestMethod.POST, permissionCode = "VMS_CREDITLIMIT")
    @Log(value = "授信额度-本地存储授信额度查询", type = LogActionType.SELECT, menuName = LogMenuEnum.CREDIT_QUOTA)
    public Result<VmsCreditlimitVO> getInfo() {
        log.info("{}本地存储授信额度查询", LOG_MSG);
        return creditLimitService.getCreditLimit();
    }

    @ApiOperation("授信额度-调整授信额度有效期")
    @AnonymousAccess(value = "/updateDate", method = RequestMethod.POST, permissionCode = "VMS_CREDIT_LIMIT_UPDATE")
    @Log(value = "授信额度-调整授信额度有效期", type = LogActionType.UPDATE, menuName = LogMenuEnum.CREDIT_QUOTA)
    public Result<Object> updateCreditLimitDateByLq() {
        TokenUser tokenUser = getUser();
        QueryCreditPeriodParams params = new QueryCreditPeriodParams();
        params.setXsfnsrsbh(vmsEnterpriseInfoService.getNsrsbh());
        params.setSxedsq(DateUtil.format(new Date(), CommonConstant.DATE_FORMAT_6));
        params.setCreateUser(tokenUser.getUserCode());
        params.setDeptCode(tokenUser.getDeptCode());
        log.info("{}调整授信额度有效期，入参：{}", LOG_MSG, JSONObject.toJSONString(params));
        return creditLimitService.updateCreditLimitDateByLq(params, tokenUser);
    }

}


